package com.mall.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.mall.pojo.ShoppingCart;
import com.mall.service.IShoppingCartService;
import com.mall.service.IWaresService;
import com.mall.utils.R;
import com.mall.vo.ShoppingCartVo;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车管理
 *
 * @author xiao-chen
 * @since 2023-04-27
 */
@RestController
@RequestMapping("/shopping-cart")
@SaCheckLogin
public class ShoppingCartController {
    @Resource
    private IShoppingCartService shoppingCartService;
    @Resource
    private IWaresService waresService;


    /**
     * 加入商品到购物车
     *
     * @param cart 加入购物车商品的信息,需要传递商品id,数量
     * @return com.mall.utils.R<java.lang.String>
     * @author cxx
     * @date 2023/4/27 21:00
     */
    @PostMapping("/save")

    public R<String> add(@RequestBody ShoppingCartVo cart) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(cart, shoppingCart);
        shoppingCart.setUserId(Long.valueOf(StpUtil.getLoginId().toString()));
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(shoppingCart.getWaresId().toString()), ShoppingCart::getWaresId, shoppingCart.getWaresId()).eq(ShoppingCart::getUserId, Long.valueOf(StpUtil.getLoginId().toString()));
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        if (ObjectUtils.isNotEmpty(one)) {
            LambdaUpdateWrapper<ShoppingCart> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(StringUtils.isNotBlank(shoppingCart.getWaresId().toString()), ShoppingCart::getWaresId, shoppingCart.getWaresId()).eq(ShoppingCart::getUserId, Long.valueOf(StpUtil.getLoginId().toString())).set(ShoppingCart::getNum, one.getNum() + cart.getNum());
            shoppingCartService.update(updateWrapper);
            return R.success("加入购物车成功!");

        }
        boolean save = shoppingCartService.save(shoppingCart);

        return save ? R.success("加入购物车成功!") : R.error("加入购物车失败!");
    }

    /**
     * 获取某个用户的购物车中的所有商品
     *
     * @return com.mall.utils.R<java.util.List < com.mall.pojo.ShoppingCart>>
     * @author cxx
     * @date 2023/4/27 21:30
     */
    @GetMapping("/get")

    public R<List<ShoppingCart>> list() {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(StpUtil.getLoginId().toString()), ShoppingCart::getUserId, Long.valueOf(StpUtil.getLoginId().toString()));
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return ObjectUtils.isNotEmpty(list) ? R.success(list) : R.error("购物车为空!");
    }

    /**
     * 在购物车内增加商品的数量，每次加一(购物车中有的东西才能使用此方法)
     *
     * @param waresId 商品id
     * @return com.mall.utils.R<java.lang.String>
     * @author cxx
     * @date 2023/4/27 21:38
     */
    @PutMapping("/add")

    public R<String> add(@RequestParam(name = "waresId") Long waresId) {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(waresId.toString()), ShoppingCart::getWaresId, waresId).eq(ShoppingCart::getUserId, Long.valueOf(StpUtil.getLoginId().toString()));
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        if (ObjectUtils.isEmpty(one)) {
            return R.error("购物车中还没有此商品!");
        }
        LambdaUpdateWrapper<ShoppingCart> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(StringUtils.isNotBlank(waresId.toString()), ShoppingCart::getWaresId, waresId).eq(ShoppingCart::getUserId, Long.valueOf(StpUtil.getLoginId().toString())).set(ShoppingCart::getNum, one.getNum() + 1);
        shoppingCartService.update(updateWrapper);
        return R.success("修改数量成功!");
    }

    /**
     * 在购物车内减少商品的数量，每次减一(购物车中有的东西才能使用此方法)
     *
     * @param waresId 商品id
     * @return com.mall.utils.R<java.lang.String>
     * @author cxx
     * @date 2023/4/27 21:47
     */
    @PutMapping("/subtract")

    public R<String> subtract(@RequestParam(name = "waresId") Long waresId) {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        LambdaUpdateWrapper<ShoppingCart> updateWrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(waresId.toString()), ShoppingCart::getWaresId, waresId).eq(ShoppingCart::getUserId, Long.valueOf(StpUtil.getLoginId().toString()));
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        if (ObjectUtils.isEmpty(one)) {
            return R.error("购物车中还没有此商品!");
        }
        if (one.getNum() == 1) {
            updateWrapper.eq(StringUtils.isNotBlank(waresId.toString()), ShoppingCart::getWaresId, waresId).eq(ShoppingCart::getUserId, Long.valueOf(StpUtil.getLoginId().toString()));
            shoppingCartService.remove(updateWrapper);
            return R.success("修改数量成功!");

        }

        updateWrapper.eq(StringUtils.isNotBlank(waresId.toString()), ShoppingCart::getWaresId, waresId).eq(ShoppingCart::getUserId, Long.valueOf(StpUtil.getLoginId().toString())).set(ShoppingCart::getNum, one.getNum() - 1);
        shoppingCartService.update(updateWrapper);
        return R.success("修改数量成功!");
    }

    /**
     * 清除某个用户的购物车商品
     *
     * @return com.mall.utils.R<java.lang.String>
     * @author cxx
     * @date 2023/4/27 22:34
     */
    @PostMapping("/clean")

    public R<String> clean() {
        shoppingCartService.remove(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getUserId, Long.valueOf(StpUtil.getLoginId().toString())));
        return R.success("清除成功!");


    }
}
